package com.gdut.simple.view.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.res.ResourcesCompat;
import android.widget.TextView;

import com.gdut.simple.R;
import com.gdut.simple.common.LogUtils;
import com.gdut.simple.view.basic.AbsBasicActivity;
import com.gdut.simple.view.fragment.ExploreFragment;
import com.gdut.simple.view.fragment.HomepageFragment;
import com.gdut.simple.view.fragment.UserFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AbsBasicActivity {

    @BindView(R.id.id_home_page_button)
    TextView mHomePageButton;
    @BindView(R.id.id_explore_button)
    TextView mExploreButton;
    @BindView(R.id.id_user_button)
    TextView mUserButton;
    private HomepageFragment mHomepageFragment;
    private ExploreFragment mExploreFragment;
    private UserFragment mUserFragment;
    private Fragment mCurrentFragment;

    private int position = 0;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.content_main, mCurrentFragment = mHomepageFragment = HomepageFragment.newInstance())
                .commitNow();
        getDataFromSavedInstanceState(savedInstanceState);
    }

    @OnClick(R.id.id_home_page_button)
    void onHomeView() {
        changeToHomepage();
    }

    @OnClick(R.id.id_explore_button)
    void onExploreView() {
        changeToExplore();
    }

    @OnClick(R.id.id_user_button)
    void onUserView() {
        changeToUser();
    }

    private void changeToHomepage() {
        if (mHomepageFragment == null) {
            mHomepageFragment = HomepageFragment.newInstance();
        }
        checkFragment(mHomepageFragment);
        position = 0;
        mHomePageButton.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_homepage_selected, 0, 0);
        mHomePageButton.setTextColor(ResourcesCompat.getColor(getResources(), R.color.colorPrimaryDark, getTheme()));
        mExploreButton.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_explore_normal, 0, 0);
        mExploreButton.setTextColor(ResourcesCompat.getColor(getResources(), R.color.gray_dark, getTheme()));
        mUserButton.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_user_normal, 0, 0);
        mUserButton.setTextColor(ResourcesCompat.getColor(getResources(), R.color.gray_dark, getTheme()));
    }

    private void changeToExplore() {
        if (mExploreFragment == null) {
            mExploreFragment = ExploreFragment.newInstance();
        }
        checkFragment(mExploreFragment);
        position = 1;
        mHomePageButton.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_homepage_normal, 0, 0);
        mHomePageButton.setTextColor(ResourcesCompat.getColor(getResources(), R.color.gray_dark, getTheme()));
        mExploreButton.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_explore_selected, 0, 0);
        mExploreButton.setTextColor(ResourcesCompat.getColor(getResources(), R.color.colorPrimaryDark, getTheme()));
        mUserButton.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_user_normal, 0, 0);
        mUserButton.setTextColor(ResourcesCompat.getColor(getResources(), R.color.gray_dark, getTheme()));
    }

    private void changeToUser() {
        if (mUserFragment == null) {
            mUserFragment = UserFragment.newInstance();
        }
        checkFragment(mUserFragment);
        position = 2;
        mHomePageButton.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_homepage_normal, 0, 0);
        mHomePageButton.setTextColor(ResourcesCompat.getColor(getResources(), R.color.gray_dark, getTheme()));
        mExploreButton.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_explore_normal, 0, 0);
        mExploreButton.setTextColor(ResourcesCompat.getColor(getResources(), R.color.gray_dark, getTheme()));
        mUserButton.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_user_selected, 0, 0);
        mUserButton.setTextColor(ResourcesCompat.getColor(getResources(), R.color.colorPrimaryDark, getTheme()));
    }

    private void checkFragment(Fragment fragment) {
        if (mCurrentFragment == fragment) {
            return;
        }
        if (!fragment.isAdded()) {
            getSupportFragmentManager().beginTransaction()
                    .hide(mCurrentFragment)
                    .add(R.id.content_main, fragment)
                    .commit();
        } else {
            getSupportFragmentManager().beginTransaction()
                    .hide(mCurrentFragment)
                    .show(fragment)
                    .commit();
        }
        mCurrentFragment = fragment;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        LogUtils.e("onSaveInstanceState");
        outState.putInt("position", position);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        LogUtils.e("onRestoreInstanceState");
        getDataFromSavedInstanceState(savedInstanceState);
    }

    private void getDataFromSavedInstanceState(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            position = savedInstanceState.getInt("position", -1);
            switch (position) {
                case 0:
                    changeToHomepage();
                    break;
                case 1:
                    changeToExplore();
                    break;
                case 2:
                    changeToUser();
                    break;
                default:
                    break;
            }
        }
    }
}
